package com.lmpop.frame.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class InitProject {
	private static SqlMapClient sqlMap = null;
	private static Properties props = null;
	public static SqlMapClient initMap(){
		if(sqlMap ==null){
			String resource = "SqlMapConfig.xml";
		    try {
		      Reader reader = Resources.getResourceAsReader(resource);
		      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		    } catch (Exception e) {
		      System.out.println(resource + " resource file read error");
		      e.printStackTrace();
		    }
		}
		return sqlMap;
	}
	public static Properties initProperties(String propertiesName){
		 String filePath = classPath() + propertiesName;
		    InputStream in = null;
		    props = new Properties();
		    try {
		      in = new BufferedInputStream(new FileInputStream(filePath));
		      props.load(in);
		    } catch (Exception e) {
		      e.printStackTrace();
		    } finally {
		      try {
		        in.close();
		      } catch (IOException e) {
		        e.printStackTrace();
		      }
		    }
		    return props;
	}
	
	public static String classPath() {
	    String basepath = Thread.currentThread().getContextClassLoader().getResource("").toString();

	    if (System.getProperty("file.separator").equals("\\")) {
	      return basepath.substring(6, basepath.length());
	    }
	    return basepath.substring(5, basepath.length());
	  }
}
